//
//  BaseTableViewController.m
//  Cat
//
//  Created by dllo on 15/11/30.
//  Copyright (c) 2015年 dllo. All rights reserved.
//

#import "BaseTableViewController.h"
#import "TableViewControllerModel.h"
#import "ZWBOneImageCell.h"
#import "CYHHotOtherVC.h"
#import "DrawAllSecVC.h"

@interface BaseTableViewController ()


@property (nonatomic, strong) NSArray *segArr;
@property(nonatomic, strong)UIView *SectionView;

@property(nonatomic, assign)NSInteger index;
@end

@implementation BaseTableViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    
    // Uncomment the following line to preserve selection between presentations.
    // self.clearsSelectionOnViewWillAppear = NO;
    
    // Uncomment the following line to display an Edit button in the navigation bar for this view controller.
    // self.navigationItem.rightBarButtonItem = self.editButtonItem;
    
    self.tableView = [[UITableView alloc] initWithFrame:CGRectMake(0, 0, VIEW_WIDTH, VIEW_HEIGHT)];
    self.tableView.separatorStyle = NO;
    [self.tableView registerClass:[ReadTableViewCell class] forCellReuseIdentifier:@"ReadCell"];
    [self.tableView registerClass:[ZWBOneImageCell class] forCellReuseIdentifier:@"OneImageCell"];
    [self.tableView registerClass:[PicTableViewCell class] forCellReuseIdentifier:@"PicCell"];
    [self.tableView registerClass:[AllTableViewCell class] forCellReuseIdentifier:@"All"];
    
    
}

- (void)createSegm:(NSInteger)i {
    if (i == 2) {
        self.segArr = [NSArray array];
         self.segArr = @[@"推荐", @"热门"];
    }else if (i == 3) {
       self.segArr =@[@"推荐", @"热门", @"最新"];
    }
   
}




- (UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section {
    self.SectionView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, VIEW_WIDTH, VIEW_HEIGHT/15)];
    self.SectionView.backgroundColor = [UIColor colorWithRed:245 / 255.0 green:245 / 255.0 blue:245 / 255.0 alpha:1];
    self.seg = [[UISegmentedControl alloc] initWithItems:self.segArr];
    self.seg.frame = CGRectMake(0, 5, VIEW_WIDTH, VIEW_HEIGHT/15-10);
    self.seg.tintColor = [UIColor clearColor];
    NSDictionary *selectedTextAttributes = @{NSFontAttributeName:[UIFont boldSystemFontOfSize:18],NSForegroundColorAttributeName:[UIColor redColor]};
    NSDictionary *unselectedTextAttributes = @{NSFontAttributeName:[UIFont boldSystemFontOfSize:18],NSForegroundColorAttributeName:[UIColor grayColor]};
    [self.seg setTitleTextAttributes:selectedTextAttributes forState:UIControlStateSelected];
    [self.seg setTitleTextAttributes:unselectedTextAttributes forState:UIControlStateNormal];
    [self.seg addTarget:self action:@selector(whichPage:) forControlEvents:UIControlEventValueChanged];
    self.seg.selectedSegmentIndex = 0;
    if (self.index == 0) {
        self.seg.selectedSegmentIndex = 0;
    }else if (self.index == 1) {
        self.seg.selectedSegmentIndex = 1;
    }else {
        self.seg.selectedSegmentIndex = 2;
    }
    
    [self.SectionView addSubview:self.seg];
    self.seg.backgroundColor = [UIColor whiteColor];
//    self.SectionView.backgroundColor = [UIColor whiteColor];
    return self.SectionView;
    
}

- (void)whichPage:(UISegmentedControl *)seg {
    
    
}

- (void)createData:(NSString *)URLStr {
    [ToolNetWorking ToolNetWorking:URLStr block:^(id result) {
        
        self.arr = [NSMutableArray array];
        self.arr = [TableViewControllerModel BaseModel:result[@"rows"]];
        [self.tableView reloadData];
    }];
}

- (CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section {
    return VIEW_HEIGHT/15;
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

#pragma mark - Table view data source

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
    // Return the number of sections.
    return 1;
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    // Return the number of rows in the section.
    return self.arr.count;
}


- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    if ([[self.arr[indexPath.row] ftype] isEqualToString:@"talk"] ) {
        ReadTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"ReadCell"];
        cell.nameLabel.text = @"";
        cell.headerImageView.image = NULL;
        
        TableViewControllerModel *model = self.arr[indexPath.row];
        cell.wordLabel.text = model.content[@"word"];
        cell.likeLabel.text = [NSString stringWithFormat:@"%@", model.likes];
        HotModel *memem = self.arr[indexPath.row];
        cell.commentsView.HeaderModel = memem;
        cell.timeLabel.text = [TimeTool Whattime:model.content[@"time"]];
        
        cell.model = memem;
        return cell;
    } else if ([[self.arr[indexPath.row] ftype] isEqualToString:@"tracing"]) {
        PicTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"PicCell" forIndexPath:indexPath];
        TableViewControllerModel *model = self.arr[indexPath.row];
        
        cell.nameLabel.text = @"";
        cell.headerImageView.image = NULL;
        
        cell.DrawNumberLabel.text = [NSString stringWithFormat:@"%@" ,model.content[@"strokes"]];
        cell.channelNameLabel.text = model.channelName;
        cell.labelsLabel.text = model.labels[0];
        cell.wordLabel.text = model.content[@"word"];
        cell.commentsLabel.text = [NSString stringWithFormat:@"%@", model.comments];
        cell.likeLabel.text = [NSString stringWithFormat:@"%@", model.likes];
        [cell.picImageView sd_setImageWithURL:[NSURL URLWithString:model.content[@"url"]] placeholderImage:[UIImage imageNamed:@"ooopic_1445565008@2x.png"]];
        HotModel *memem = self.arr[indexPath.row];
        cell.commentsView.HeaderModel = memem;
        
        cell.model = memem;
        return cell;
    }else if ([[self.arr[indexPath.row] ftype] isEqualToString:@"pic"] || [[self.arr[indexPath.row] ftype] isEqualToString:@"handDrawn"]) {
        ZWBOneImageCell *cell = [tableView dequeueReusableCellWithIdentifier:@"OneImageCell"forIndexPath:indexPath];
         TableViewControllerModel *model = self.arr[indexPath.row];
        
        cell.nameLabel.text = @"";
        cell.headerImageView.image = NULL;
        
        NSArray *array = [model.content [@"url"]  componentsSeparatedByString:@","];
        cell.arr = array;
        cell.likeLabel.text = [NSString stringWithFormat:@"%@", model.likes];
        cell.commentsLabel.text = [NSString stringWithFormat:@"%@", model.comments];
        cell.wordLabel.text = model.content[@"word"];
        cell.timeLabel.text = [TimeTool Whattime:model.content[@"time"]];
        HotModel *memem = self.arr[indexPath.row];
        cell.commentsView.HeaderModel = memem;
        cell.model = memem;
        return cell;

    } else {
//        if ([[self.arr[indexPath.row] ftype] isEqualToString:@"album"])
        AllTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"All"forIndexPath:indexPath];
        TableViewControllerModel *model = self.arr[indexPath.row];
        
        cell.nameLabel.text = @"";
        cell.headerImageView.image = NULL;
        
        [cell.headerImageView sd_setImageWithURL:[NSURL URLWithString:model.content[@"url"]] placeholderImage:[UIImage imageNamed:@"ooopic_1445565008@2x.png"]];
        cell.nameLabel.text = model.content[@"title"];
        cell.wordLabel.text = model.content[@"word"];
        cell.numberLabel.text = [NSString stringWithFormat:@"共%@幅作品", model.content[@"albumPnum"]];
        return cell;
    }

    

//    OneImageTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"OneImageCell" forIndexPath:indexPath];
//    PicTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"PicCell" forIndexPath:indexPath];

}

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath {
    NSDictionary *dic = [NSDictionary dictionaryWithObjectsAndKeys:[UIFont systemFontOfSize:15], NSFontAttributeName, nil];
    // 根据文本字体大小来计算文本的cell高度
    // 第一个参数: 文字显示的最大尺寸范围(375, 0)(0 代表最大的意思)
    // 第二个参数:
    CGRect rect = [[self.arr[indexPath.row] content][@"word"] boundingRectWithSize:CGSizeMake(VIEW_WIDTH, 0) options:NSStringDrawingUsesLineFragmentOrigin attributes:dic context:nil];
    
    if ([[self.arr[indexPath.row] ftype] isEqualToString:@"tracing"]) {
        
        return VIEW_HEIGHT / 2 + 230 + rect.size.height;
    } else if ([[self.arr[indexPath.row] ftype] isEqualToString:@"talk"]) {
        
        return 200 + rect.size.height;
    }else if ([[self.arr[indexPath.row] ftype] isEqualToString:@"pic"]) {
        
        return VIEW_HEIGHT/2 + rect.size.height;
    }
    else if ([[self.arr[indexPath.row] ftype] isEqualToString:@"album"]) {
        
        return 150;
    }
    else if ([[self.arr[indexPath.row] ftype] isEqualToString:@"handDrawn"]) {
        
        return VIEW_HEIGHT / 2 + rect.size.height;
    }
    return 100;
}

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
    if ([[self.arr[indexPath.row] ftype] isEqualToString:@"tracing"]) {
        
        CYHHotOtherVC *otherVC = [[CYHHotOtherVC alloc] init];
        HotModel *model = self.arr[indexPath.item];
        otherVC.Secid = model.secid;
        [self.navigationController pushViewController:otherVC animated:YES];
        
    } 
    else if ([[self.arr[indexPath.row] ftype] isEqualToString:@"album"]) {
        
        DrawAllSecVC *secVC = [[DrawAllSecVC alloc] init];
        
        TableViewControllerModel *model = self.arr[indexPath.row];
        secVC.secid = model.secid;
        [self.navigationController pushViewController:secVC animated:YES];
    }

}






/*
// Override to support conditional editing of the table view.
- (BOOL)tableView:(UITableView *)tableView canEditRowAtIndexPath:(NSIndexPath *)indexPath {
    // Return NO if you do not want the specified item to be editable.
    return YES;
}
*/

/*
// Override to support editing the table view.
- (void)tableView:(UITableView *)tableView commitEditingStyle:(UITableViewCellEditingStyle)editingStyle forRowAtIndexPath:(NSIndexPath *)indexPath {
    if (editingStyle == UITableViewCellEditingStyleDelete) {
        // Delete the row from the data source
        [tableView deleteRowsAtIndexPaths:@[indexPath] withRowAnimation:UITableViewRowAnimationFade];
    } else if (editingStyle == UITableViewCellEditingStyleInsert) {
        // Create a new instance of the appropriate class, insert it into the array, and add a new row to the table view
    }   
}
*/

/*
// Override to support rearranging the table view.
- (void)tableView:(UITableView *)tableView moveRowAtIndexPath:(NSIndexPath *)fromIndexPath toIndexPath:(NSIndexPath *)toIndexPath {
}
*/

/*
// Override to support conditional rearranging of the table view.
- (BOOL)tableView:(UITableView *)tableView canMoveRowAtIndexPath:(NSIndexPath *)indexPath {
    // Return NO if you do not want the item to be re-orderable.
    return YES;
}
*/

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
